package com.ljx.vod.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljx.glkt.exception.GgktException;
import com.ljx.glkt.utils.Result;
import com.ljx.model.vod.Teacher;
import com.ljx.vo.vod.TeacherQueryVo;
import com.ljx.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ljx
 * @since 2023-04-03
 */
//@CrossOrigin
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有讲师
     * @return
     */
    @ApiOperation("所有讲师列表")
    @GetMapping("findAll")
    public Result<List<Teacher>> findAllTeacher(){
        //测试自定义异常
//        try {
//            int a = 10/0;
//        }catch(Exception e) {
//            throw new GgktException(20001,"出现自定义异常");
//        }
        List<Teacher> list = teacherService.list();
        return Result.ok(list).message("查询成功");

    }
    //删除讲师
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable String id){
        boolean isSuccess = teacherService.removeById(id);
        if(isSuccess) return Result.ok("成功");
        else return Result.fail("失败");
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param teacherQueryVo
     * @return
     */
    @ApiOperation(value = "获取分页列表")
    @PostMapping("findQueryPage/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) TeacherQueryVo teacherQueryVo) {
            if(teacherQueryVo == null){
                List<Teacher> list = teacherService.list();
                return Result.ok(list);
            }else {
                Integer level = teacherQueryVo.getLevel();
                String name = teacherQueryVo.getName();
                String joinDateBegin = teacherQueryVo.getJoinDateBegin();
                String joinDateEnd = teacherQueryVo.getJoinDateEnd();
                //封装分页参数
                Page<Teacher> pageParam = new Page<>(page, limit);
                //封装条件
                QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
                if(!StringUtils.isEmpty(level)){
                    queryWrapper.eq("level",level);
                }
                if(!StringUtils.isEmpty(name)){
                    queryWrapper.like("name",name);
                }
                if(!StringUtils.isEmpty(joinDateBegin)){
                    queryWrapper.ge("join_date",joinDateBegin);
                }
                if (!StringUtils.isEmpty(joinDateEnd)){
                    queryWrapper.le("join_data",joinDateEnd);
                }
                IPage<Teacher> teacherPage = teacherService.page(pageParam, queryWrapper);
                return Result.ok(teacherPage);
            }
    }

    /**
     * 添加讲师
     * @param teacher
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result savaTeacher(@RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return Result.ok(null);
    }

    /**
     * 根据id获取讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }

    /**
     * 修改讲师
     * @param teacher
     * @return
     */
    @ApiOperation(value = "修改讲师")
    @PutMapping("teacher/update")
    public Result updateTeacher(@RequestBody Teacher teacher){
        teacherService.updateById(teacher);
        return Result.ok("更新成功");
    }

    /**
     * 批量删除
     * @param idList
     * @return
     */
    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        teacherService.removeByIds(idList);
        return Result.ok(null);
    }

}

